package com.honglang.aop.impl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.web.context.WebApplicationContext;

import com.honglang.common.tools.FreeMarkStaticPageFactory;
import com.honglang.module.article.service.BlogArticleManager;
import com.honglang.struts2.action.BaseAction;


@Aspect
public class FreeMarkStaticPageFactoryAspect {
	private static final Logger logger = Logger.getLogger(FreeMarkStaticPageFactoryAspect.class);
	
	private FreeMarkStaticPageFactory freeMarkStaticPageFactory;
	
	@Pointcut("execution(* com.honglang..action.*.*(..))")
	protected void sideBarPagePointCut(){};
	
	@After("sideBarPagePointCut() && " +"@annotation(com.honglang.aop.impl.UpdateSideBarHtmlPointCut)")
	public void createSideBarHtmlPage(JoinPoint jp){
		logger.info("Begin create sideBar html file");
		BaseAction baseAction = (BaseAction)jp.getTarget();
		HttpServletRequest request =  baseAction.getRequest();
		HttpServletResponse response =  baseAction.getResponse();
		WebApplicationContext  webApplicationContext = baseAction.getWebApplicationContext();
		BlogArticleManager blogArticleManager = (BlogArticleManager)webApplicationContext.getBean("blogArticleManager");
		Map<Object,Object> map = blogArticleManager.initSideBar();
		String htmlDirectory = request.getSession().getServletContext().getRealPath("/");
		this.getFreeMarkStaticPageFactory().createHtmlPage(map, "sideBar.ftl", htmlDirectory + "sideBar.html", request, response);
		logger.info("SideBar html file be created");
	}

	
	
	public FreeMarkStaticPageFactory getFreeMarkStaticPageFactory() {
		return freeMarkStaticPageFactory;
	}

	public void setFreeMarkStaticPageFactory(
			FreeMarkStaticPageFactory freeMarkStaticPageFactory) {
		this.freeMarkStaticPageFactory = freeMarkStaticPageFactory;
	}

	
	
}
